package com.yunfeng.supercall.fragment;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ly.library.stickyadpter.BaseStickyListAdapter;
import com.ly.quickdev.library.view.MySideBar;
import com.ly.quickdev.library.view.MySideBar.OnTouchingLetterChangedListener;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.AdsBean;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.manager.AdsManager;
import com.yunfeng.supercall.manager.ManagerFactory;

public abstract class BaseStickyHeaderListFragment<T extends ContactsBaseBean>
		extends BaseFragment implements OnItemClickListener {
	protected List<T> mList;
	protected BaseStickyListAdapter<T> mAdapter;
	protected StickyListHeadersListView mListView;
	private MySideBar mSideBar;
	protected View mLoadingView;

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mList = new ArrayList<T>();
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_sticky_header_list;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mSideBar = findViewById(R.id.side_bar);
		mLoadingView = findViewById(R.id.loading);
		mSideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

			@Override
			public void onTouchingLetterChanged(String s) {
				// TODO Auto-generated method stub
				for (int i = 0, j = mList.size(); i < j; i++) {
					ContactsBaseBean base = mList.get(i);
					if (base.getStickyItem().equalsIgnoreCase(s)) {
						mListView.setSelection(i);
						return;
					}
				}
			}
		});
		mListView = (StickyListHeadersListView) view.findViewById(R.id.list);
		AdsManager mAdsManager = (AdsManager) ManagerFactory
				.createManager(AdsBean.class);
		mAdsManager.addAdsHeader(mListView.getWrappedList(), 2);
		mListView.setOnItemClickListener(this);
		loadData();
	}

	public abstract void loadData();

	public void setListAdapter() {
		// if (mAdapter == null) {
		mAdapter = new BaseStickyListAdapter<T>(getActivity(), mList) {

			@Override
			public View initView(int position, View convertView,
					ViewGroup parent) {
				// TODO Auto-generated method stub
				return BaseStickyHeaderListFragment.this.getView(position,
						convertView, parent);
			}

			@Override
			public View getHeaderView(int position, View convertView,
					ViewGroup parent) {
				// TODO Auto-generated method stub
				return BaseStickyHeaderListFragment.this.getHeaderView(
						position, convertView, parent);
			}

			@Override
			public int getViewTypeCount() {
				// TODO Auto-generated method stub
				return BaseStickyHeaderListFragment.this.getViewTypeCount();
			}

			@Override
			public int getItemViewType(int position) {
				// TODO Auto-generated method stub
				return BaseStickyHeaderListFragment.this
						.getItemViewType(position);
			}
		};
		mListView.setAdapter(mAdapter);
		mListView.setVisibility(View.VISIBLE);
		mLoadingView.setVisibility(View.GONE);
		// } else {
		// mAdapter.notifyDataSetChanged();
		// }
	}

	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		return ContactsBaseBean.getHeaderView(getActivity(), position, convertView,
				parent, mList);
	}

	public int getViewTypeCount() {
		return 1;
	}

	public int getItemViewType(int position) {
		return 0;
	}

}
